//
//  WeekBizDateEditVC.swift
//  SBSafetyApp
//
//  Created by Aimeow on 2022/11/28.
// 履职管理-周营业日

import Foundation

import Foundation
import UIKit

class WeekBizDateEditVC: SubLevelViewController {
    let scrollV = UIScrollView()

    var month =  Date().string(format: "MM")
    var year =  Date().string(format: "yyyy")
    
    init(withDeptId _deptId: Int64? = nil, month: String, year: String) {
        super.init(nibName: nil, bundle: nil)
        self.deptId = _deptId
        self.month = month
        self.year = year
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .bg
        title = "周营业日"
        setupUI()
    }
    
    @objc func submitButtonClicked(sender: UIButton) {
        let workDate = dateSelectPanel.selectWeekDays
        let arrays: [String] = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
        
        var workDatas: [String] = []
        var statuss: [String] = []

        for item in arrays {
            workDatas.append(item)
            if workDate.contains(item) {
                statuss.append("1")
            } else {
                statuss.append("0")
            }
        }
        
        let workDatasStr = workDatas.joined(separator: ",")
        let statussStr = statuss.joined(separator: ",")

        view.showToastActivity()
        API.xgyyzt(withParam: XgyyztParam(deptId: self.deptId ?? BSUser.currentUser.deptId, month: month, remark: contentTextView.textView.text, status: statussStr, type: "week", workDates: workDatasStr, year: year)) { responseModel in
            
            if responseModel.errorCode == .Success {
                Utils.delay(second: 5) {
                    DispatchQueue.main.async {
                        self.view.hideToastActivity()
                        self.navigationController?.popViewController(animated: true)
                    }
                }
            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    func setupUI() {
        scrollV.keyboardDismissMode = .interactive
        view.addSubview(scrollV)
        scrollV.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            make.left.right.equalToSuperview()
            make.width.equalTo(ScreenWidth)
        }
        
        scrollV.addSubview(dateSelectPanel)
        scrollV.addSubview(contentTextView)
        scrollV.addSubview(submitButton)
        
        dateSelectPanel.snp.makeConstraints { make in
            make.top.equalTo(scrollV.snp.top)
            make.left.equalTo(view).offset(10)
            make.right.equalTo(view).offset(-10)
        }

        contentTextView.snp.makeConstraints { make in
            make.top.equalTo(dateSelectPanel.snp.bottom).offset(10)
            make.left.equalTo(view).offset(10)
            make.right.equalTo(view).offset(-10)
        }
        submitButton.snp.makeConstraints { make in
            make.left.equalTo(view).offset(10)
            make.right.equalTo(view).offset(-10)
            make.top.equalTo(contentTextView.snp.bottom).offset(20)
            make.height.equalTo(50)
            make.bottom.equalTo(scrollV.snp.bottom).offset(-40)
        }
    }
    
    lazy var dateSelectPanel: EditBizDateView = {
        let panel = EditBizDateView(withStyle: .Style2, withTitle: "营业时间")
        return panel
    }()
    
    lazy var contentTextView: BaseTextViewPanel = {
        let panel = BaseTextViewPanel(withStyle: .Style2, withTitle: "说明")
        return panel
    }()
    
    lazy var submitButton: UIButton = {
        let view = UIButton.createCornerButton("保存修改")
        view.addTarget(self, action: #selector(submitButtonClicked(sender:)), for: .touchUpInside)
        return view
    }()
    
}
